package com.ymk.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.ymk.dao.MemberDao;
import com.ymk.pojo.Member;
import com.ymk.service.MemberService;
import com.ymk.util.DateUtils;
import com.ymk.util.MD5Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service(interfaceClass = MemberService.class)
@Transactional
public class MemberServiceImpl implements MemberService {

    @Autowired
    MemberDao memberDao;

    @Override
    public Member findByTelephone(String telephone) {
        return memberDao.findByTelephone(telephone);
    }

    @Override
    public void add(Member member) {
        if(member.getPassword() != null){
            member.setPassword(MD5Utils.md5(member.getPassword()));
        }
        memberDao.add(member);
    }

    //根据月份统计会员数量
    @Override
    public List<Integer> findMemberCountByMonth(List<String> list) {
        List<Integer> memberCountList = new ArrayList<>();
        if(list != null && list.size() > 0){
            for (String month : list) {
                //String regTime = months + "-31"
                //获取指定月份的最后一天
                String regTime = DateUtils.getLastDayOfMonth(month);
                //迭代过去12个月，每个月注册会员的数量，根据注册日期查询
                Integer memberCount =   memberDao.findMemberCountBeforeDate(regTime);
                memberCountList.add(memberCount);
            }
        }
        return memberCountList;
    }
}
